// commons/diy/goods-list/index.js
var is_get_goods = 1;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    update_cat: {
      type:Object,
      value: {}
    },
    tabs: {
      type:Object,
      value: {}
    },
    goods_list: {
      type:Object,
      value: {}
    },
    activity_status: {
      type:String,
      value: ''
    },
    loadingStatus: {
      type:Number,
      value: 0
    },
    all_color: {
      type:String,
      value: '#f90000'
    },
    headheight: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentTab: 0,
    update_cat: null,
    show_label: 1,
    currentData: 0,
    page: 1,
  },
  ready: function (){
    this.getDiyInfo()
    this.setTime()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getDiyInfo: function () {
      var that = this;
      var shop = wx.getStorageSync('address_info');
      var shop_id = shop && shop.shop_id ? shop.shop_id : 0
      var item = {
        name: 'cat',
        shop_id: shop_id
      };
      getApp().request({
        url: getApp().api.index.get_diy_info,
        data: item,
        success: function(t) {
          that.data.cat_id = 0;
          if ( t.data.list.activity_goods != null 
            && t.data.list.activity_goods != [] 
            && t.data.list.activity_goods != '' 
            && t.data.list.activity_goods != undefined
            ) {
            that.setData({
              activity_status: "now_goods",
              goods_list: t.data.list.activity_goods.now_goods.goods_list,
            })
          } else {
            that.setData({
              tabs: t.data.list.cat_list,
              goods_list: t.data.list.cat_list[0].goods_list,
            })
          }
        },
        complete: function() {
          
        }
      })
    },
    swichNav: function(e) {
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        this.setData({
          currentTab: e.target.dataset.current,
        })
  
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
          navScrollLeft: (this.data.currentTab - 2) * singleNavWidth
        })
      }
      var cat_id = e.currentTarget.id,that = this;;
      this.data.cat_id = cat_id;
      var page = this.data.tabs[cat_id].page ? this.data.tabs[cat_id].page : 1;
      this.setData({
        goods_list: this.data.tabs[cat_id].goods_list ? this.data.tabs[cat_id].goods_list : [],
        page: page,
        cat_id: cat_id,
        loadingStatus: 0
      },function (){
        that.lower()
      })
    },
    alert_info: function (t) {
      console.log('ddddd');
    },
    setTime() {
      var that =  this;
      setInterval(function () {
        var date = new Date();
        var now = date.getTime() / 1000;
        now = Math.round(now);
        var update_cat = that.data.update_cat ? that.data.update_cat : that.properties.update_cat;
        if (update_cat.is_show == 1 && update_cat.default_huodong == 1){
          var let_time = update_cat.activity_end_html - now;
          var start_time = update_cat.activity_start_html - now;
          if (start_time > 0) {
            var d = Math.floor(start_time / 60 / 60 / 24);
            var h = Math.floor(start_time / 60 / 60 % 24);
            var m = Math.floor(start_time / 60 % 60);
            var s = Math.floor(start_time % 60);
            update_cat.s = s < 10 ? "0" + s : s;
            update_cat.m = m < 10 ? "0" + m : m;
            update_cat.h = h < 10 ? "0" + h : h;
            update_cat.d = d < 10 ? "0" + d : d;
            update_cat.time_type = 2; //还没开始
          } else if (let_time >= 0) {
            var d = Math.floor(let_time / 60 / 60 / 24);
            var h = Math.floor(let_time / 60 / 60 % 24);
            var m = Math.floor(let_time / 60 % 60);
            var s = Math.floor(let_time % 60);
            update_cat.s = s < 10 ? "0" + s : s;
            update_cat.m = m < 10 ? "0" + m : m;
            update_cat.h = h < 10 ? "0" + h : h;
            update_cat.d = d < 10 ? "0" + d : d;
            update_cat.time_type = 1; // 还没结束
    
          } else {
            update_cat.s = '00';
            update_cat.m = '00';
            update_cat.h = '00';
            update_cat.d = '00';
            update_cat.time_type = 0; // 已经结束
          }
        }
        that.setData({
          update_cat: update_cat,
        })


      }, 1000)
    },
    showpicker: function (t) {
      this.triggerEvent('showpicker',t.currentTarget.dataset)
    },
    update_status: function(tt) {
      var status = tt.currentTarget.dataset['status'];
      this.setData({
        activity_status: status,
        currentData: tt.target.dataset.current
      })
  
    },
    // 商品下拉加载
    lower: function() {
      var that = this;
      var cat_id = this.data.cat_id;
      var tabs = that.data.tabs;
      this.setData({
        loadingStatus: 1
      })
      if (is_get_goods == 0) {
        return false;
      }
      if (that.data.activity_status != 0) {
        var pages = this.data.page + 1
      }else {
        var pages = tabs[cat_id].page ? tabs[cat_id].page + 1 : 2
      }
      is_get_goods = 0;
      getApp().request({
        url: getApp().api.default.get_goods,
        data: {
          page: pages,
          cat_id: tabs && tabs[cat_id] ? tabs[cat_id].id : 0,
          shop_id: this.data.shop_id,
          activity_status: this.data.activity_status ? this.data.activity_status : '',
          is_index:1
        },
        success: function(res) {
          if (res.length > 0) {
            if (that.data.activity_status != 0) {
              var page = that.data.page + 1
              var activity_goods = that.data.activity_goods;
              var goods_list = that.data.goods_list;
              goods_list = goods_list.concat(res);
              that.setData({
                goods_list: goods_list,
                loadingStatus: 0,
                page: page
              })
            } else {
              console.log(tabs);
              var goods_list = tabs && tabs[cat_id] && tabs[cat_id].goods_list  ? tabs[cat_id].goods_list : [];
              goods_list = goods_list.concat(res);
              tabs[cat_id].goods_list = goods_list;
              tabs[cat_id].page = pages;
              that.setData({
                tabs: tabs,
                goods_list: goods_list,
                loadingStatus: 0,
              })
            }
          } else {
            that.setData({
              loadingStatus: 2
            })
          }
        },
        complete: function() {
          is_get_goods = 1;
        }
      });
    },
  }
})
